﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SqlScriptVault.Model
{
    [Serializable]
    public class ExecutionHistory : IHistory
    {
        private readonly ScriptVaultExecution _scriptVaultExecution;

        public ExecutionHistory(ScriptVaultExecution scriptVaultExecution)
        {
            _scriptVaultExecution = scriptVaultExecution;

            if (_scriptVaultExecution.AllSucceeded)
            {
                ImageName = "../Images/green-check-icon.png";
            }
            else
            {
                ImageName = "../Images/red-cross-icon.png";
            }

            EventDate = DateTime.Now;
        }

        public string ImageName { get; private set; }

        public DateTime EventDate { get; private set; }

        public string EventName
        {
            get 
            {
                StringBuilder messageBuilder = new StringBuilder("Script execution ");

                if (_scriptVaultExecution.AllSucceeded)
                {
                    messageBuilder.AppendFormat("succeeded ({0} scripts executed).", _scriptVaultExecution.Scripts.Count());
                }
                else
                {
                    messageBuilder.AppendFormat("failed ({0}).", _scriptVaultExecution.FailureMessage);
                }

                return messageBuilder.ToString();
            }
        }
    }
}
